cmake_minimum_required(VERSION 3.10)

if(NOT CMAKE_BUILD_TYPE)
    #set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build." FORCE)
    message("unknown build type, use Debug")
    set(CMAKE_BUILD_TYPE "Debug")
endif()

if(NOT CMAKE_EXPORT_COMPILE_COMMANDS)
    set(CMAKE_EXPORT_COMPILE_COMMANDS "TRUE")
endif()

if(NOT CMAKE_C_COMPILER_WORKS)
    set(CMAKE_C_COMPILER_WORKS "true")
endif()

if(NOT CMAKE_CXX_COMPILER_WORKS)
    set(CMAKE_CXX_COMPILER_WORKS "true")
endif()


if(CMAKE_BUILD_TYPE MATCHES "Debug")
    message(">>>>>>>>>>>>>>>>>> CMAKE_BUILD_TYPE is ${CMAKE_BUILD_TYPE}")
    if(UNIX AND APPLE)
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
    endif()
elseif(CMAKE_BUILD_TYPE MATCHES "Release")
    message(">>>>>>>>>>>>>>>>>> CMAKE_BUILD_TYPE is ${CMAKE_BUILD_TYPE}")
    if(UNIX AND APPLE)
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3")
    endif()
else()
    # Handle other build types
    message(STATUS "Unknown build type")
endif()

# 判断编译器
if(APPLE)
    set(CMAKE_C_COMPILER "clang")  # 设置Clang++编译器路径
    set(CMAKE_CXX_COMPILER "clang++")
    set(CMAKE_C_FLAGS "-std=c99")
    set(CMAKE_CXX_FLAGS "-std=c++11")
    # set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
elseif(UNIX AND NOT APPLE)
    set(CMAKE_CXX_COMPILER "path/to/g++")  # 设置G++编译器路径
elseif(WIN32)
    set(CMAKE_CXX_COMPILER "path/to/MSVC")  # 设置MSVC编译器路径
    message(">> using Visual Studio C++")
else()
    message(FATAL_ERROR "Unsupported platform")
endif()

# 设置项目名称
project(double_tool)

# 生成文件名称
# set(OUT_FILE_NAME S1Max)
set(OUT_FILE_NAME EG01)
add_definitions(-DCONFIG_XML_FILE_NAME="config_eg01.xml")


if(APPLE)
    set(CMAKE_OSX_ARCHITECTURES "x86_64")
    set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13")
    set(CMAKE_XCODE_ATTRIBUTE_WRAPPER_EXTENSION "app")
    set(CMAKE_XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.example.${OUT_FILE_NAME}")
    set(CMAKE_XCODE_ATTRIBUTE_PRODUCT_NAME ${OUT_FILE_NAME})
    # set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
    set(WXWIDGETS_LIBRARYS_PATH /Users/develop/Documents/xiaoyong/librarys/wxwidgets/Release)
    set(WXWIDGETS_INCLUDE_PATH /Users/develop/Documents/xiaoyong/librarys/wxwidgets/include)
    set(LIBUSB_LIBRARYS_PATH /Users/develop/Documents/xiaoyong/librarys/libusb/xcode)
    set(LIBUSB_INCLUDE_PATH /Users/develop/Documents/xiaoyong/librarys/libusb/xcode)

    add_definitions(-D_FILE_OFFSET_BITS=64 -DWX_PRECOMP -D_UNICODE -D__WXOSX_COCOA__ -D__WXMAC__ -D__WXOSX__)
    set(LIBRARYS_USE
        pthread -Wl
        usb-1.0
        wx_baseu-3.2
        wx_baseu_xml-3.2
        wxregexu-3.2
        wxpng-3.2
        wxtiff-3.2
        wxjpeg-3.2
        wxpng-3.2
        wxtiff-3.2
        wxjpeg-3.2
        wx_osx_cocoau_aui-3.2
        wx_osx_cocoau_core-3.2
        expat
        z
        iconv
        "-framework CoreFoundation"
        "-framework Security"
        "-framework Carbon"
        "-framework Cocoa"
        "-framework IOKit"
        "-framework QuartzCore"
        "-framework AudioToolbox"
        "-framework WebKit"
        "-framework Security"
        )
elseif(UNIX AND NOT APPLE)
elseif(WIN32)
    # 编译窗口程序
    set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS /NODEFAULTLIB:LIBCMT /LTCG" )
    # 库路径
    set(WXWIDGETS_PATH D:/librarys/wxWidgets)
    set(LIBUSB_PATH D:/librarys/libusb)
    set(WXWIDGETS_LIBRARYS_PATH ${WXWIDGETS_PATH}/lib/vc_x64_lib)
    set(WXWIDGETS_INCLUDE_PATH ${WXWIDGETS_PATH}/include ${WXWIDGETS_LIBRARYS_PATH}/mswud)
    set(LIBUSB_LIBRARYS_PATH ${LIBUSB_PATH}/VS2022/MS64/static)
    set(LIBUSB_INCLUDE_PATH ${LIBUSB_PATH}/include)
    set(WIN32_LIBRARYS wxpng
    wxtiff
    wxzlib
    wxjpeg
    wxexpat
    kernel32
    user32
    gdi32
    comdlg32
    winspool
    winmm
    shell32
    shlwapi
    comctl32
    ole32
    oleaut32
    uuid
    rpcrt4
    advapi32
    version
    ws2_32
    wininet
    oleacc
    uxtheme
    bthprops
    )
    if(CMAKE_BUILD_TYPE MATCHES "Debug")
        set(WX_LIB_SUFFIX ud)
    elseif(CMAKE_BUILD_TYPE MATCHES "Release")
        set(WX_LIB_SUFFIX u)
    else()
        message(STATUS "Unknown build type")
        set(WX_LIB_SUFFIX uddd)
    endif()

    set(LIBRARYS_USE  ${WIN32_LIBRARYS} wxbase32${WX_LIB_SUFFIX} wxmsw32${WX_LIB_SUFFIX}_core wxmsw32${WX_LIB_SUFFIX}_html wxregex${WX_LIB_SUFFIX} wxbase32${WX_LIB_SUFFIX}_xml wxmsw32${WX_LIB_SUFFIX}_aui libusb-1.0)
    add_definitions(-D_UNICODE -D_CRT_SECURE_NO_WARNINGS)
else()
    message(FATAL_ERROR "Unsupported platform")
endif()


if(MSVC)
    if(CMAKE_BUILD_TYPE MATCHES "Debug")
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MDd /Zi /Ob0 /Od /RTC1")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MDd /Zi /Ob0 /Od /RTC1")
    elseif(CMAKE_BUILD_TYPE MATCHES "Release")
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MD /O2 /Ob2 /GL /DNDEBUG")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MD /O2 /Ob2 /GL /DNDEBUG")
    else()
    endif()
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /source-charset:utf-8 /execution-charset:utf-8")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /source-charset:utf-8 /execution-charset:utf-8")

endif(MSVC)




# 指定源文件
set(SOURCE_FILES src/main.cpp
                 src/panel_logger.cpp
                 src/panel_equalizer.cpp
                 src/panel_reverb.cpp
                 src/panel_chorus.cpp
                 src/panel_delay.cpp
                 src/panel_distortion.cpp
                #  src/panel_compress.cpp
                 src/panel_wah.cpp
                 src/panel_preset.cpp
                 src/panel_compressor.cpp
                 src/ctrl_equalizer.cpp
                 src/ctrl_knob.cpp
                 src/filter.c
                 src/MainFrame.cpp
                 src/USBCommunication.cpp
                 src/commandprocess.cpp
                 src/DataSave.cpp
)

# 添加多个头文件目录
include_directories(${CMAKE_SOURCE_DIR}/src
                    ${WXWIDGETS_INCLUDE_PATH}
                    ${LIBUSB_INCLUDE_PATH}
)

# 添加多个库文件目录
link_directories(${WXWIDGETS_LIBRARYS_PATH}
                 ${LIBUSB_LIBRARYS_PATH}
)

# 添加可执行文件
add_executable(${OUT_FILE_NAME} ${SOURCE_FILES})

if(APPLE)
    set_target_properties(${OUT_FILE_NAME} PROPERTIES MACOSX_BUNDLE YES)
    # set_target_properties(${OUT_FILE_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/src/Info.plist)
    set_target_properties(${OUT_FILE_NAME} PROPERTIES MACOSC_BUNDLE_NAME ${OUT_FILE_NAME})
    set_target_properties(${OUT_FILE_NAME} PROPERTIES MACOSC_BUNDLE_GUI_IDENTIFIER "com.double.${OUT_FILE_NAME}")
    set_target_properties(${OUT_FILE_NAME} PROPERTIES MACOSC_BUNDLE_LONG_VERSION_STRING "1.0.0")
    # set_target_properties(${OUT_FILE_NAME} PROPERTIES MACOSC_BUNDLE_ICON_FILE "icon.icns")
endif()
# 添加链接选项
target_link_options(${OUT_FILE_NAME} PRIVATE ${LINKOPTION})

# 添加多个库文件
target_link_libraries(${OUT_FILE_NAME} ${LIBRARYS_USE})


# Release 模式
# target_link_libraries(${OUT_FILE_NAME} wxmsw32u_core wxbase32u)


# 在编译成功后，自动执行拷贝MyProject.exe文件到指定目录下
# add_custom_command(TARGET ${OUT_FILE_NAME} POST_BUILD
#                    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${OUT_FILE_NAME}> ${CMAKE_SOURCE_DIR})

# 添加自定义命令，在编译后复制DLL文件
# add_custom_command(TARGET ${OUT_FILE_NAME} POST_BUILD
#                    COMMAND ${CMAKE_COMMAND} -E copy_directory
#                    ${CMAKE_SOURCE_DIR}/dll_dir $<TARGET_FILE_DIR:${OUT_FILE_NAME}>)
